Skip to content

ASoC: SOF: ipc4-control: Use local copy of IPC message for sending#5737

Open
ujfalusi wants to merge 1 commit intothesofproject:topic/sof-devfrom
ujfalusi:peter/sof/pr/ipc4-control-init-race
Open

ASoC: SOF: ipc4-control: Use local copy of IPC message for sending#5737
ujfalusi wants to merge 1 commit intothesofproject:topic/sof-devfrom
ujfalusi:peter/sof/pr/ipc4-control-init-race

Conversation

@ujfalusi
Copy link
Copy Markdown
Collaborator

@ujfalusi ujfalusi commented Apr 16, 2026

If a kcontrol update comes to a control right at the same time when the PCM containing the control is started up then there is a small window when a race can happen:
while the widget is set up the swidget->setup_mutex is taken in topology level and if the control update comes at this point, it will be stopped within sof_ipc4_set_get_kcontrol_data() with the mutex and it will be blocked until the swidget setup is done, which will clear the control's IPC message payload.

To avoid such race, use local copy of the template IPC message instead of the template directly. This will ensure data integrity in case of concurrent updates during initialization.

Link: #5734

Copilot AI review requested due to automatic review settings April 16, 2026 06:37
@ujfalusi ujfalusi linked an issue Apr 16, 2026 that may be closed by this pull request
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Moves IPC4 kcontrol message payload/param configuration into sof_ipc4_set_get_kcontrol_data() so it happens under swidget->setup_mutex, preventing races between control updates and widget/PCM startup.

Changes:

  • Extended sof_ipc4_set_get_kcontrol_data() to accept payload pointer/size and a param_id override.
  • Updated volume, generic, and bytes control paths to pass payload/config directly into the helper instead of mutating cdata->msg in the callers.
  • Removed per-caller clearing of msg->data_ptr/msg->data_size after IPC operations.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sound/soc/sof/ipc4-control.c Outdated
Comment on lines 59 to 61
msg->data_ptr = data;
msg->data_size = data_size;
ret = iops->set_get_data(sdev, msg, msg->data_size, set);
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

msg->data_ptr and msg->data_size are now set here but never reset. Several callers pass stack or temporary heap buffers (e.g. &params, kzalloc()ed payloads) that are freed/invalid after this call, leaving a dangling pointer in cdata->msg that can be observed/reused later. Clear msg->data_ptr/msg->data_size (and, if applicable, restore any overridden fields) before releasing setup_mutex to avoid stale state and potential UAF if future code paths reuse the message template without reinitializing these fields.

Copilot uses AI. Check for mistakes.
Comment thread sound/soc/sof/ipc4-control.c Outdated
Comment on lines +56 to +57
if (param_id)
msg->extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(param_id);
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

param_id is treated as “optional” via if (param_id), which breaks bytes controls when the ABI header’s type (used as param_id) is 0. sof_ipc4_bytes_put() copies sof_abi_hdr from userspace, so data->type can legitimately become 0; in that case this function will leave msg->extension set to the previous non-zero param_id and send the request with the wrong parameter ID. Use an explicit flag to indicate whether to override the param_id (or always apply the provided param_id, including 0, and have non-bytes callers pass a separate sentinel/flag when they want to preserve the template extension).

Copilot uses AI. Check for mistakes.
@ujfalusi ujfalusi force-pushed the peter/sof/pr/ipc4-control-init-race branch 2 times, most recently from ece5125 to d50d9d5 Compare April 16, 2026 07:50
If a kcontrol update comes to a control right at the same time when the
PCM containing the control is started up then there is a small window when
a race can happen:
while the widget is set up the swidget->setup_mutex is taken in topology
level and if the control update comes at this point, it will be stopped
within sof_ipc4_set_get_kcontrol_data() with the mutex and it will be
blocked until the swidget setup is done, which will clear the control's
IPC message payload.

To avoid such race, use local copy of the template IPC message instead of
the template directly. This will ensure data integrity in case of
concurrent updates during initialization.

Link: thesofproject#5734
Signed-off-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
@ujfalusi ujfalusi changed the title ASoC: SOF: ipc4-control: Move the message setup within mutex protection ASoC: SOF: ipc4-control: Use local copy of IPC message for sending Apr 16, 2026
@ujfalusi
Copy link
Copy Markdown
Collaborator Author

Changes since v1:

  • use local copy of the template IPC message instead of directly using the template itself.

Comment thread sound/soc/sof/ipc4-control.c
Copy link
Copy Markdown
Collaborator

@kv2019i kv2019i left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FW reported error: 9 - Specified resource not found

4 participants